package dao.impl;

import bean.AirInfo;
import dao.AirInfoDao;
import util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AirInfoDaoImpl extends DBUtils implements AirInfoDao {
    @Override
    public List<AirInfo> getAll() {
        List<AirInfo> airInfos = new ArrayList<>();
        try {
            String sql = "select * from airinfo";
            List list = new ArrayList();
            ResultSet rs = query(sql, list);
            while (rs.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setAirNo(rs.getString("airNo"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setDate(rs.getDate("date"));
                airInfos.add(airInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfos;
    }

    @Override
    public List<AirInfo> getByDate(Date date) {
        List<AirInfo> airInfos = new ArrayList<>();
        try {
            String sql = "select * from airinfo where date=?";
            List list = new ArrayList();
            list.add(date);
            ResultSet rs = query(sql, list);
            while (rs.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setAirNo(rs.getString("airNo"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setDate(rs.getDate("date"));
                airInfos.add(airInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfos;
    }

    @Override
    public List<AirInfo> getByDestination(String destination) {
        List<AirInfo> airInfos = new ArrayList<>();
        try {
            String sql = "select * from airinfo where destination=?";
            List list = new ArrayList();
            list.add(destination);
            ResultSet rs = query(sql, list);
            while (rs.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setAirNo(rs.getString("airNo"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setDate(rs.getDate("date"));
                airInfos.add(airInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfos;
    }

    @Override
    public boolean deleteAir(int id) {
        boolean result = false;
        try {
            String sql = "delete from airinfo where id=?";
            List list = new ArrayList();
            list.add(id);
            int rows = update(sql, list);
            if (rows >0) {
                result = true;
            }
        } finally {
            closeAll();
        }
        return result;
    }

    @Override
    public boolean updateAir(AirInfo airInfo) {
        boolean result = false;
        try {
            String sql = "update airinfo  set airno=? ,destination= ?, date=? where id = ? ";
            List list = new ArrayList();
            list.add(airInfo.getAirNo());
            list.add(airInfo.getDestination());
            list.add(airInfo.getDate());
            list.add(airInfo.getId());
            int rows = update(sql, list);
            if (rows >0) {
                result = true;
            }
        } finally {
            closeAll();
        }
        return result;
    }













}
